package com.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class SessionFilter implements Filter {
	private Logger log = Logger.getLogger(this.getClass());
	private String objName;
	
	public String getObjName() {
		return objName;
	}

	public void setObjName(String objName) {
		this.objName = objName;
	}

	public void destroy() {

	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException
	{
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String currentURL = request.getRequestURI();
		//log.info("currentURL======"+currentURL);
		String targetURL =null;
		if(currentURL.lastIndexOf("/")==0){
			targetURL=currentURL;
		}else{
		targetURL = currentURL.substring(currentURL.indexOf("/", 1),
				currentURL.length());
		}
		//log.info("targetURL======"+targetURL);
		
		if(!targetURL.contains("login")
			&&!targetURL.contains("randImg.jsp")
			&&!targetURL.contains("error.jsp")
			&&!targetURL.contains("dwr")
			&&!targetURL.contains("common")){
			//验证用户登陆=========
			Object obj = request.getSession().getAttribute(objName);
			if(obj==null){
				
			}else{
				
			}
			filterChain.doFilter(request, response);
		}else{
			filterChain.doFilter(request, response);
		}
		
		
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		objName = filterConfig.getInitParameter("objName");
	}

}
